﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Protocol;
using System.Web.Security;
using Common;
namespace LogicAndDB
{
    public partial class CUser
    {
        #region
        //判断用户注册结果
        public enum RegisterResult
        {
            Success, EmailAlreadyRegister, Failed, EmailNotExist
        }

        //找回密码时用户验证结果
        public enum AuthResult
        {
            Success, Failed, EmailNotExist
        }

        /// <summary>
        /// 用于检验邮箱激活
        /// Pass:验证通过 NoExistUser: 用户不存在 EffectiveTime: 连接超时 ActivateFial;验证失败
        /// </summary>
        public enum ActivationResult
        {
            Pass, NoExistUser, OverTime, ActivateFial
        }

        #endregion
        public static CUser GetUserByMail(string mail)
        {
            var ret =DBContainer.Instance.UserList.Where(p => p.Mail.Equals(mail,StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
            return ret;
        }

        public static CUser GetUserByName(string userName)
        {
            var ret = DBContainer.Instance.UserList.Where(p => p.Name.Equals(userName,StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
            return ret;
        }

        public static CUser GetUserById(int id)
        {
            var ret = DBContainer.Instance.UserList.Where(p => p.ID == id).SingleOrDefault();
            return ret;
        }

        public void SetNewPassword(string password)
        {
            this.Password = Util.SHA1Encrypt(password);
           
        }
        public bool IsPasswordCorrect(string password)
        {
            string sha_password = Util.SHA1Encrypt(password);
            return sha_password == this.Password;
        }


        public static bool Auth(string usrName,string password)
        {
            CUser usr = GetUserByName(usrName);
            if (usr == null)
                return false;
            return usr.IsPasswordCorrect(password);
            
        }
        /// <summary>
        /// hard code ,多国语言问题都要解决。
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public static  LoginResp Auth(LoginReq req)
        {
            LoginResp ret = new LoginResp();
            ret.DetailMessage = "无此用户";
            if (req == null || req.Password == null || req.UserName == null)
            {
                ret.AckCode = LoginResp.LOGINACK_FAIL;
                return ret;
            }

            string sha_password = Util.SHA1Encrypt(req.Password);
            CUser user = GetUserByMail(req.UserName);
            if (user == null)
            {
                ret.AckCode = LoginResp.LOGINACK_FAIL;
            }
            else
            {
                if (user.IsPasswordCorrect(req.Password))
                {
                    ret.AckCode = LoginResp.LOGINACK_SUCCESS;
                }
                else
                {
                    ret.AckCode = LoginResp.LOGINACK_FAIL;
                }
                ret.DetailMessage = "";
            }
            return ret;
        }
 

        static public bool IsNameDuplicated(string name)
        {
            return GetUserByName(name) != null;
        }
        
        static public bool IsEmailDuplicated(string email)
        {
            return GetUserByMail(email) != null;
        }

        public bool IsDataComplete()
        {
            bool ret = !Util.IsStringEmpty(Mail);
            ret=ret&&(!Util.IsStringEmpty(Password));

            return ret;
        }

        public static bool AddNewUser(CUser user)
        {
            try
            {
                user.Password = Util.SHA1Encrypt(user.Password);
                DBContainer.Instance.UserList.AddObject(user);
           
                DBContainer.Instance.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;

            
        }

    }
}
